package logic

import (
	"context"
	"errors"
	"taobao-merchandise/mysql/models"

	"taobao-merchandise/merchandiserpc/internal/svc"
	"taobao-merchandise/merchandiserpc/merchandiserpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type ShopUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopUpdateLogic {
	return &ShopUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品修改
func (l *ShopUpdateLogic) ShopUpdate(in *merchandiserpc.ShopUpdateRequest) (*merchandiserpc.ShopUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var shop models.Shop
	models.DB.Debug().Where("id = ?", in.ShopId).Find(&shop)
	if shop.ID == 0 {
		return nil, errors.New("当前商品不存在,修改失败")
	}

	shop = models.Shop{
		Model:  gorm.Model{ID: uint(in.ShopId)},
		Title:  in.Title,
		Image:  in.Image,
		Price:  float64(in.Price),
		UserId: in.UserId,
	}
	models.DB.Debug().Updates(&shop)

	return &merchandiserpc.ShopUpdateResponse{
		ShopId: int64(shop.ID),
	}, nil
}
